<?php namespace App\Http\Middleware;



use App\Models\User;
use App\Models\Admin_group_perm;

use Closure, Config, Request, Session, Res;
class AdminMenuMiddleware {

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next) {
        $user = User::leftJoin('admin_group_member', 'users.id', '=', 'admin_group_member.user_id')
                ->leftJoin('admin_group', 'admin_group_member.group_id', '=', 'admin_group.group_id')
                ->where('users.id', Session::get('admin.id'))
                ->select('users.*','admin_group_member.*', 'admin_group_member.user_id as admin_user_id','admin_group.groupname')
                ->first();
//        var_dump($user);exit;
        Session::put('admin.user', $user->toArray());
        
        $perm = [];
        if(!empty($user->group_id)){
            $perm = Admin_group_perm::where('group_id',$user->group_id)->lists('perm')->toArray();
        }
        $isfounder = Session::get('admin.isfounder');
        if (!empty($isfounder)) {
            Session::put('admin.user.groupname', '超级管理员');
            $perm = 'all';
        }
        Session::put('admin.perm', $perm);
        $new_menu = [];        
        $all_perm = Config::get('perm');
//        var_dump($perm);exit;
        $allow = 0;
        $currentRoute = $request->path();
        Session::put('admin.currentRoute', $currentRoute);
        $action_menu = [];
        foreach ($all_perm as $k => $v) {            
            if (!empty($v)) {
                $sub = '';
                foreach ($v as $k2 => $v2) {
                    if($k == 'index'){
                        $new_menu[$k] = $v2;                        
                        if($currentRoute == $v2){
                            $allow = 1;
                            $action_menu['p'] = $k;
                            $action_menu['s'] = '';
                        }
                        $menu_to_url[$k] = $v2;
                    }else{
                        if (!is_array($v2)) {
                            if ($k2 == 'url') {
                                if (!empty($isfounder) || in_array($v2, $perm)) {
                                    if(!empty($perm[$k]['menu'])){
                                        $new_menu[$k] = $perm[$k]['url'];
                                    }                                    
                                    if ($currentRoute == $v2) {
                                        $allow = 1;
                                        $action_menu['p'] = $k;
                                        $action_menu['s'] = '';
                                    }
                                    $menu_to_url[$k] = $v2;
                                }
                            }
                        } else {
                            if(!empty($isfounder) || in_array($v2['url'], $perm)){
                                if($currentRoute == $v2['url']){
                                    $allow = 1;
                                    $action_menu['p'] = $k;
                                    $action_menu['s'] = $v2['name'];
                                }
                                $menu_to_url[$v2['name']] = $v2['url'];
                                if(!empty($v2['menu'])){
                                    if (!empty($v2['parent'])) {
                                        if ($v2['parent'] == '1') {
                                            $sub = $v2['name'];
                                            $new_menu[$k][$v2['name']]['url'] = $v2['url'];
                                        } elseif ($v2['parent'] == '2') {
                                            $sub = '';
                                        }
                                    } else {
                                        if (!empty($sub)) {
                                            $new_menu[$k][$sub]['sub'][$v2['name']] = $v2['url'];
                                        } else {
                                            $new_menu[$k][$v2['name']] = $v2['url'];
                                        }
                                    }
                                }
                            }
                        }
                    }                    
                }
            }
        }

        if(empty($allow)){
            return Res::showMessage('error',trans('errors.No_Permission'));
        }
        Session::put('admin.menu', $new_menu);
        Session::put('admin.action_menu', $action_menu);
        Session::put('admin.menu_to_url', $menu_to_url);
        return $next($request);
    }

}